package com.jd.risk.bankorder.service.privilege;

import java.util.ArrayList;
import java.util.List;


import com.jd.risk.bankorder.dao.privilege.UserMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jd.risk.bankorder.common.CommonResult;
import com.jd.risk.bankorder.entity.privilege.Resource;
import com.jd.risk.bankorder.entity.privilege.Role;
import com.jd.risk.bankorder.entity.privilege.User;
import com.jd.risk.bankorder.entity.privilege.UserExample;

@Service
public class UserService {
	
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private  ResourceService resourceService;
	
	@Autowired
	private PasswordHelper passwordHelper;
	
	public CommonResult<User> getAllUser()
	{
		CommonResult<User> result = new CommonResult<User>();
		UserExample userExample = new UserExample();
		List<User> users =  userMapper.selectByExample(userExample);
		Long count = userMapper.countByExample(userExample);
		result.setRows(users);
		result.setTotal(count);
		result.setSuccess(true);
		result.setMessage("success");
		return result;
	}

	public CommonResult<String> userAdd(User user) {
		
		CommonResult<String> result = new CommonResult<String>();
		user.setPassword("11111111");
		int res = userMapper.insertSelective(user);
		if(res >0)
		{
			result.setSuccess(true);
		}else{
			result.setSuccess(false);
			result.setMessage("失败");
		}
		return result;
	}

	public User getUser(Long id) {
		return userMapper.selectByPrimaryKey(id);
	}

	public int userAuthorization(User user) {
		
		int ret = userMapper.updateByPrimaryKeySelective(user);
		
		return ret;
	}

	public User findByUsername(String username) {
		UserExample example = new UserExample();
		example.createCriteria().andUsernameEqualTo(username);
		List<User> users = userMapper.selectByExample(example);
		if(CollectionUtils.isNotEmpty(users))
		{
			return users.get(0);
		}
		return null;
	}

	public List<String> getUrlByUsername(String username) {
		
		User user = findByUsername(username);
		List<String> urls = new ArrayList<String>();
		if(null !=user)
		{
			String roleids = user.getRoleIds();
			if(StringUtils.isNotBlank(roleids))
			{
				String[] roleidseg = roleids.split(",");
				for(String roleid :roleidseg)
				{
					if(StringUtils.isNotBlank(roleid))
					{
						Role role = roleService.getRoleByRoleId(Long.parseLong(roleid));
						String resourceStr = role.getResourceIds();
						if(StringUtils.isNotBlank(resourceStr))
						{
							String[] resourceseg = resourceStr.split(",");
							for(String resourceId :resourceseg)
							{
								Resource resource = resourceService.getResourceById(Long.parseLong(resourceId));
								if(StringUtils.isNotBlank(resource.getUrl()) && !resource.getUrl().equals("#"))
								{
									urls.add(resource.getUrl());
								}
								
							}
						}
					}
				}
			}
		}
		
		return urls;
	}

	public int updateUser(User user) {
		passwordHelper.encryptPassword(user);
		return userMapper.updateByPrimaryKeySelective(user);
	}
}
